package logic

import (
	"context"
	"fmt"
	"time"
	"user_srv/cache"
	"user_srv/models"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelAddLogic {
	return &NovelAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 小说发布
func (l *NovelAddLogic) NovelAdd(in *user.NovelAddRequest) (*user.NovelAddResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	novels := models.Novels{
		Title:  in.Title,
		Writer: in.Writer,
		Hot:    in.Hot,
		Image:  in.Image,
		Price:  float64(in.Price),
		Num:    in.Num,
	}
	if err = novels.NovelAdd(); err != nil {
		logx.Error("小说添加失败")
		return nil, fmt.Errorf("小说添加失败")
	}
	cache.Rdb.Set(l.ctx, "NovelAdd"+in.Title, novels, time.Minute*10)
	return &user.NovelAddResponse{
		Id: int64(novels.ID),
	}, nil
}
